package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.bean.ApproveDetailsBean;

import java.util.List;


public class ApprovePictureVideoVoiceAdapter extends RecyclerView.Adapter<ApprovePictureVideoVoiceAdapter.ActivityHolder> implements View.OnClickListener {

    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<ApproveDetailsBean.DataBean.AuditTaskAttachEntityListBean> mData;
    private boolean isTablet = false;
    public ApprovePictureVideoVoiceAdapter(Context context, List<ApproveDetailsBean.DataBean.AuditTaskAttachEntityListBean> data, boolean isTablet) {
        this.context = context;
        this.mData = data;
        this.isTablet = isTablet;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_picture_video_voice, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(context, view, isTablet);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                ApproveDetailsBean.DataBean.AuditTaskAttachEntityListBean eventAttachEntitiesBean = mData.get(position);
                String attachUrl = eventAttachEntitiesBean.getUrl();
                boolean isUrlHasImage = PictureMimeType.isUrlHasImage(attachUrl);
                Glide.with(context)
                        .load(attachUrl)
                        .apply(BaseApplication.getRequestOptions())
                        .into(holder.riv);
                if (isUrlHasImage) {
                    holder.ivStartButton.setVisibility(View.GONE);
                } else {
                    holder.ivStartButton.setVisibility(View.VISIBLE);
                }
            }
        }

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void upData(List<ApproveDetailsBean.DataBean.AuditTaskAttachEntityListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private LinearLayout layout;
        private FrameLayout layoutFrame;
        private ShapeableImageView riv;
        private ImageView ivStartButton;

        public ActivityHolder(Context context, View itemView, boolean isTablet) {
            super(itemView);

            layout = (LinearLayout) itemView.findViewById(R.id.layout);
            layoutFrame = (FrameLayout) itemView.findViewById(R.id.layout_frame);
            riv = (ShapeableImageView) itemView.findViewById(R.id.riv);
            ivStartButton = (ImageView) itemView.findViewById(R.id.iv_start_button);

            ViewGroup.LayoutParams lp = layoutFrame.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth();
            int mWidth = (screenWidth - SizeUtils.dp2px(20) * 2 - SizeUtils.dp2px(15) * 4) / 3;
            lp.width = mWidth;
            lp.height = mWidth;
            layoutFrame.setLayoutParams(lp);
        }
    }

}
